home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2005 June (DVD) / DPPRO0605DVD.iso / Install / program files / Borland / BDS / 3.0 / Demos / CSharp / Applications / Dice / Dice1.cs < prev    next >
Encoding:
Text File  |  2004-10-22  |  7.0 KB  |  219 lines

  1. ∩╗┐using System;
  2. using System.Collections;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Windows.Forms;
  7.  
  8. // Die Component o5/2oo3 by stjo@borland.com
  9. //
  10. // To run this Sample :
  11. // 1┬░) Open Die.bdsproj and compile it to create Die.dll
  12. // 2┬░) Install the component (Menu Components, Installed .Net Components, Select an Assembly : Die.dll)
  13. // 3┬░) Open Dice.bdsproj and run it !
  14.  
  15. namespace Die
  16. {
  17.     /// <summary>
  18.     /// Summary description for UserControl1.
  19.     /// </summary>
  20.     public class Dice : System.Windows.Forms.UserControl
  21.     {
  22.         /// <summary>
  23.         /// Required designer variable.
  24.         /// </summary>
  25.         private System.ComponentModel.Container components = null;
  26.         private System.Windows.Forms.PictureBox pictureBox1;
  27.         private System.Windows.Forms.PictureBox pictureBox3;
  28.         private System.Windows.Forms.PictureBox pictureBox4;
  29.         private System.Windows.Forms.PictureBox pictureBox5;
  30.         private System.Windows.Forms.PictureBox pictureBox6;
  31.         private System.Windows.Forms.PictureBox pictureBox7;
  32.         private System.Windows.Forms.PictureBox pictureBox9;
  33.  
  34.         // Specifies the random engine
  35.         // to launch the dice
  36.         private System.Random random;
  37.  
  38.         // Determines the number displayed
  39.         // on the dice from 1 to 6
  40.         private System.Int32 _face;
  41.         public System.Int32 face {
  42.             get { return _face; }
  43.             set {
  44.                 if (value>=1 && value<=6) {
  45.                     _face=value;
  46.                     display();
  47.                 } else {
  48.                     throw new Exception("Dice must be from 1 to 6");
  49.                 }
  50.             }
  51.         }
  52.  
  53.         // Allows of not the dice to be launched
  54.         private System.Boolean _active=true;
  55.         public System.Boolean active {
  56.             get { return _active; }
  57.             set {
  58.                 _active=value;
  59.                 // And changes dice background color
  60.                 BackColor=_active?
  61.                      System.Drawing.SystemColors.ActiveCaptionText
  62.                     :System.Drawing.SystemColors.InactiveBorder;
  63.             }
  64.         }
  65.  
  66.         public void launch()
  67.         {
  68.             if (active) {
  69.                 // Get a random face number
  70.                 face=random.Next(6)+1;
  71.             }
  72.         }
  73.  
  74.         public void display()
  75.         {
  76.             // Show/Hide points
  77.             pictureBox1.Visible=(face>1);
  78.             pictureBox3.Visible=(face>=4);
  79.             pictureBox4.Visible=(face==6);
  80.             pictureBox5.Visible=(face==1 || face==3 || face==5);
  81.             pictureBox6.Visible=(face==6);
  82.             pictureBox7.Visible=(face>=4);
  83.             pictureBox9.Visible=(face>1);
  84.         }
  85.  
  86.         public Dice()
  87.         {
  88.             // This call is required by the Windows.Forms Form Designer.
  89.             random=new System.Random();
  90.             InitializeComponent();
  91.             launch();
  92.             // TODO: Add any initialization after the InitForm call
  93.         }
  94.  
  95.         /// <summary>
  96.         /// Clean up any resources being used.
  97.         /// </summary>
  98.         protected override void Dispose( bool disposing )
  99.         {
  100.             if( disposing )
  101.             {
  102.                 if( components != null )
  103.                     components.Dispose();
  104.             }
  105.             base.Dispose( disposing );
  106.         }
  107.  
  108.         #region Component Designer generated code
  109.         /// <summary>
  110.         /// Required method for Designer support - do not modify
  111.         /// the contents of this method with the code editor.
  112.         /// </summary>
  113.         private void InitializeComponent()
  114.         {
  115.             System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Dice));
  116.             this.pictureBox1 = new System.Windows.Forms.PictureBox();
  117.             this.pictureBox3 = new System.Windows.Forms.PictureBox();
  118.             this.pictureBox4 = new System.Windows.Forms.PictureBox();
  119.             this.pictureBox5 = new System.Windows.Forms.PictureBox();
  120.             this.pictureBox6 = new System.Windows.Forms.PictureBox();
  121.             this.pictureBox7 = new System.Windows.Forms.PictureBox();
  122.             this.pictureBox9 = new System.Windows.Forms.PictureBox();
  123.             this.SuspendLayout();
  124.             //
  125.             // pictureBox1
  126.             //
  127.             this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
  128.             this.pictureBox1.Location = new System.Drawing.Point(8, 56);
  129.             this.pictureBox1.Name = "pictureBox1";
  130.             this.pictureBox1.Size = new System.Drawing.Size(16, 16);
  131.             this.pictureBox1.TabIndex = 0;
  132.             this.pictureBox1.TabStop = false;
  133.             this.pictureBox1.Visible = false;
  134.             //
  135.             // pictureBox3
  136.             //
  137.             this.pictureBox3.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox3.Image")));
  138.             this.pictureBox3.Location = new System.Drawing.Point(56, 56);
  139.             this.pictureBox3.Name = "pictureBox3";
  140.             this.pictureBox3.Size = new System.Drawing.Size(16, 16);
  141.             this.pictureBox3.TabIndex = 2;
  142.             this.pictureBox3.TabStop = false;
  143.             this.pictureBox3.Visible = false;
  144.             //
  145.             // pictureBox4
  146.             //
  147.             this.pictureBox4.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox4.Image")));
  148.             this.pictureBox4.Location = new System.Drawing.Point(8, 32);
  149.             this.pictureBox4.Name = "pictureBox4";
  150.             this.pictureBox4.Size = new System.Drawing.Size(16, 16);
  151.             this.pictureBox4.TabIndex = 3;
  152.             this.pictureBox4.TabStop = false;
  153.             this.pictureBox4.Visible = false;
  154.             //
  155.             // pictureBox5
  156.             //
  157.             this.pictureBox5.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox5.Image")));
  158.             this.pictureBox5.Location = new System.Drawing.Point(32, 32);
  159.             this.pictureBox5.Name = "pictureBox5";
  160.             this.pictureBox5.Size = new System.Drawing.Size(16, 16);
  161.             this.pictureBox5.TabIndex = 4;
  162.             this.pictureBox5.TabStop = false;
  163.             this.pictureBox5.Visible = false;
  164.             //
  165.             // pictureBox6
  166.             //
  167.             this.pictureBox6.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox6.Image")));
  168.             this.pictureBox6.Location = new System.Drawing.Point(56, 32);
  169.             this.pictureBox6.Name = "pictureBox6";
  170.             this.pictureBox6.Size = new System.Drawing.Size(16, 16);
  171.             this.pictureBox6.TabIndex = 5;
  172.             this.pictureBox6.TabStop = false;
  173.             this.pictureBox6.Visible = false;
  174.             //
  175.             // pictureBox7
  176.             //
  177.             this.pictureBox7.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox7.Image")));
  178.             this.pictureBox7.Location = new System.Drawing.Point(8, 8);
  179.             this.pictureBox7.Name = "pictureBox7";
  180.             this.pictureBox7.Size = new System.Drawing.Size(16, 16);
  181.             this.pictureBox7.TabIndex = 6;
  182.             this.pictureBox7.TabStop = false;
  183.             this.pictureBox7.Visible = false;
  184.             //
  185.             // pictureBox9
  186.             //
  187.             this.pictureBox9.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox9.Image")));
  188.             this.pictureBox9.Location = new System.Drawing.Point(56, 8);
  189.             this.pictureBox9.Name = "pictureBox9";
  190.             this.pictureBox9.Size = new System.Drawing.Size(16, 16);
  191.             this.pictureBox9.TabIndex = 8;
  192.             this.pictureBox9.TabStop = false;
  193.             this.pictureBox9.Visible = false;
  194.             //
  195.             // Dice
  196.             //
  197.             this.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
  198.             this.Controls.Add(this.pictureBox9);
  199.             this.Controls.Add(this.pictureBox7);
  200.             this.Controls.Add(this.pictureBox6);
  201.             this.Controls.Add(this.pictureBox5);
  202.             this.Controls.Add(this.pictureBox4);
  203.             this.Controls.Add(this.pictureBox3);
  204.             this.Controls.Add(this.pictureBox1);
  205.             this.Name = "Dice";
  206.             this.Size = new System.Drawing.Size(80, 80);
  207.             this.DoubleClick += new System.EventHandler(this.Dice_DoubleClick);
  208.             this.ResumeLayout(false);
  209.         }
  210.  
  211.         private void Dice_DoubleClick(object sender, System.EventArgs e)
  212.         {
  213.              active=!active;
  214.         }
  215.  
  216.         #endregion
  217.     }
  218. }
  219.